package com.hdey.controller.chao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdey.mapper.chao.WuziMapper;
import com.hdey.pojo.chao.Wuzi;
import com.hdey.service.chao.WuziService;
import com.hdey.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/wz")
@Slf4j
@ResponseBody
@Configuration
public class WuziController {

        @Resource
        private WuziService wuziService;
        @Resource
        private WuziMapper wuziMapper;
        @GetMapping("/index")
        public ModelAndView index(){

            return new ModelAndView("chao/Wuzi");

        }

        @RequestMapping("/search")
        @ResponseBody
        public Result search(Integer page, Integer limit, String cgName){
                Page<Wuzi> wuziPage = wuziService.searchPage(page, limit, cgName);
                System.out.println(wuziPage.getRecords());
                return Result.success(wuziPage);
        }

        @RequestMapping("/search1")
        @ResponseBody
        public Result search(){
                List<Wuzi> wuziList = wuziService.searchPage1();
                return Result.success(wuziList);
        }

        // 添加
        @RequestMapping("/add")
        public Result add(Wuzi wuzi) {
                Random random=new Random();
                String a = String.valueOf((Math.random() * 9 + 1) * 10000);
                String numStr = a.toString().substring(0, 4);
                wuzi.setCreateDate(LocalDate.now());
                wuzi.setCgCode(numStr);
                boolean flag = wuziService.save(wuzi);
                return flag ? Result.success("添加成功") : Result.failed("添加失败");
        }


        // 删除
        @RequestMapping("/del")
        public Result del(Integer dictionaryId){
                System.out.println(dictionaryId);

                LambdaQueryWrapper<Wuzi> wrapper=new LambdaQueryWrapper<>();
                wrapper.eq(Wuzi::getDictionaryId,dictionaryId);
                 int  flag = wuziMapper.delete(wrapper);
//                String msg = "";
//
//                if(flag){
//                        msg = "删除成功!";
//                }else {
//                        msg = "删除失败";
//                }
//                return Result.fail(msg);
                return flag > 0 ? Result.success("删除成功") : Result.failed("删除失败");
        }

        @RequestMapping("/update")
        public Result update(Wuzi wuzi){
                boolean flag=wuziService.updateById(wuzi);
                String msg=null;
                if (flag){
                        msg="修改成功";
                }else {
                        msg="修改失败";
                }
                return Result.failed(msg);
        }

        /*@GetMapping("/del")
        public Result del(Integer id){
                try {
                        keshiService.del(id);
                        msg="角色删除成功!";
                        return Result.success(msg);
                }catch (Exception ex){
                        msg="角色删除失败!";
                        log.error(msg,ex);
                        return Result.fail(msg);
                }
        }*/



}
